<!DOCTYPE html>
<html lang="en">
<title>企业储值卡管理</title>
<#import "/layout/layout.html" as body/>

<@body.page>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>企业储值卡管理</h2>
        <ol class="breadcrumb">
            <li><a href="#">企业储值卡管理</a></li>
            <li class="active"><strong>企业储值账户列表</strong></li>
        </ol>
    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title overflow">
                    <h5 class="pull-left">企业储值卡账户列表</h5>
                    <a href="${baseUrl}system/company/add_step1" class="btn btn-sm btn-primary pull-right"
                       style="margin-left: 10px; ">新增企业</a>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <form class="form-inline" role="form" id="form">
                            <div class="form-group">
                                <label>企业名称：</label>
                                <input class="form-control" type="text" id="name" name="name"/>
                            </div>
                            <button type="button" id="search" class="btn btn-outline btn-primary"
                                    style="margin-top: 5px">确认
                            </button>
                        </form>
                        <hr style="width: 100%">
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>企业ID</th>
                                <th>企业名称</th>
                                <th>员工数量</th>
                                <th>账户余额</th>
                                <th>当月限制</th>
                                <th>当月限制余额</th>
                                <th>当月消耗</th>
                                <th>历史消耗</th>
                                <th>企业管理员</th>
                                <th>客户经理</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="list">
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="ibox-footer overflow">
                    <h4 class="pull-left">合计：<span id="count">0</span>个企业用户</h4>
                    <div id="copot-page" class="pull-right"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="${baseUrl}js/main.js"></script>
<script>
    //搜索
    $("#search").click(function () {
        companyPage(1)
    });

    $(document).ready(function () {
        companyPage(1);
    });
    function companyPage(currentIndex) {
        var param = {
            "pageIndex": currentIndex,
            "pageSize": 10,
            "name": $("#name").val()
        };
        $.post("${baseUrl}system/company/companyList", param, function (result) {
            console.log("JSON " + result);
            result = JSON.parse(result);
            console.log("JSON.parse后" + result);
            //分页
            console.log("-------");
            new Page({
                parent: $("#copot-page"),
                nowPage: currentIndex,
                pageSize: 10,
                totalCount: result.count,
                setting: {
                    defaultPage: 5,//默认显示可点击分页的个数
                    firstPageText: "首页",
                    prevPageText: "上一页",
                    nextPageText: "下一页",
                    lastPageText: "尾页"
                },
                callback: function (nowPage, totalPage) {
                    companyPage(nowPage);
                }
            });

            $("#count").text(result.count);

            var html = "";
            var status;
            var currentLeftAmount;
            var monthlyTotalLimit;
            var monthlyLimitLeftAmount;
            $.each(result.list, function (i, data) {
                if (data) {
                    if (data.status >= 0) {
                        if (data.status == 0) {
                            status = "系统录入"
                        }
                        if (data.status == 1) {
                            status = "正常"
                        }
                        if (data.status == 2) {
                            status = "冻结"
                        }
                    }
                    currentLeftAmount = null;
                    if (data.currentLeftAmount > 0) {
                        currentLeftAmount = data.currentLeftAmount;
                    } else {
                        currentLeftAmount = 0;
                    }
                    if (data.monthlyTotalLimit > 0) {
                        monthlyTotalLimit = data.monthlyTotalLimit;
                    } else {
                        monthlyTotalLimit = '无限制';
                    }
                    if (data.monthlyTotalLimit > 0) {
                        monthlyLimitLeftAmount = data.monthlyLimitLeftAmount;
                    } else {
                        monthlyLimitLeftAmount = '无限制';
                    }
                }
                html += "<tr><td>" + data.id + "</td>";
                html += "<td>" + data.name + "</td>";
                html += "<td>全部(" + data.staffTotalCount + ")</br>正常(" + data.staffNormalCount + ")</td>";
                html += "<td>¥" + currentLeftAmount + "</td>";
                html += "<td>¥" + monthlyTotalLimit + "</td>";
                html += "<td>¥" + monthlyLimitLeftAmount + "</td>";
                html += "<td>¥" + data.currentCostAmount + "</td>";
                html += "<td>¥" + data.totalCostAmount + "</td>";
                html += "<td>" + data.manager + "</br>" + data.managerMobile + "</td>";
                html += "<td>" + data.clientManager + "</br>" + data.clientManagerMobile + "</td>";
                html += "<td>" + status + "</td>";
                html += "<td>";

                html += "<a href='${baseUrl}system/company/empl_list?companyId=" + data.id + "' class='btn btn-default'><i class='fa fa-group'></i>员工</a>";
                html += "<a href='${baseUrl}system/company/order_list?companyId=" + data.id + "' class='btn btn-default'><i class='fa fa-folder'></i>订单</a>";
                html += "<a href='${baseUrl}system/company/recharge?id=" + data.id + "' class='btn btn-default'><i class='fa fa-rmb'></i> 充值</a>";
                html += "<a href='${baseUrl}system/company/add_step1?id=" + data.id + "' class='btn btn-default'><i class='fa fa-gear'></i>设置</a>";
                if (data.status == 1) {
                    html += '<a href="javascript:void(0)" onclick="lockCompany(\'' + data.id + '\')" class="btn btn-default"><i class="fa fa-lock"></i> 冻结</a>';
                } else if (data.status == 2) {
                    html += '<a href="javascript:void(0)" onclick="unlockCompany(\'' + data.id + '\')" class="btn btn-default"><i class="fa fa-unlock"></i>解冻</a>';
                }

                html += "</td>";
                html += "</tr>";
            });
            $("#list").html(html);

        });
    }

    /**
     * 冻结企业
     *
     * @param companyId 企业ID
     */
    function lockCompany(companyId) {
        swal({
            title: "确定要冻结该企业吗?",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "容我三思！",
            confirmButtonText: "冻结!"
        }).then(function () {
            $.post("${baseUrl}system/company/lockCompany?id=" + companyId, function (result) {
                if (result && result == "success") {

                    swal("冻结成功!", "页面即将重新加载,请稍后...", "success");
                    window.setTimeout(function () {
                        window.location.reload(true);
                    }, 1000);
                } else {
                    swal("错误!", result, "error");
                }
            });
        });
    }
    /**
     * 解冻企业
     *
     * @param companyId 企业ID
     */
    function unlockCompany(companyId) {
        swal({
            title: "确定要解冻该企业吗?",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "容我三思！",
            confirmButtonText: "解冻!"
        }).then(function () {
            $.post("${baseUrl}system/company/unlockCompany?id=" + companyId, function (result) {
                if (result && result == "success") {

                    swal("冻结成功!", "页面即将重新加载,请稍后...", "success");
                    window.setTimeout(function () {
                        window.location.reload(true);
                    }, 1000);
                } else {
                    swal("错误!", result, "error");
                }
            });
        });
    }

</script>
</@body.page>
</html>